<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElInput, FormInstance, FormRules } from 'element-plus'
import { required } from '@/utils/formRules'
import { ucDeptListApi, ucIpOwnerSeeApi, ucOrgDeptUserListApi, ucOrgListApi } from '@/api/cy/Guanxi'

// 获取字典
import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
const dict: any = wsCache.get('dict')

/********************** 变量 start ******************************/
const ruleFormRef = ref<FormInstance>()

const rules = reactive<FormRules>({
  province: [required],
  ahb_org_id: [required],
  ahb_dept_id: [required],
  ahb_user_id: [required],
  admin_org_id: [required],
  admin_dept_id: [required],
  admin_user_id: [required],
  terminal_type: [required]
})

/****************************** 变量 end ******************************/
let data: any = ref({
  province: '',
  city: '',
  terminal_type: '',
  ahb_org_id: '',
  ahb_dept_id: '',
  ahb_user_id: '',
  admin_org_id: '',
  admin_dept_id: '',
  admin_user_id: ''
})

let datas = reactive({
  ahb_org_id: [],
  ahb_dept_id: [],
  ahb_user_id: [],
  admin_org_id: [],
  admin_dept_id: [],
  admin_user_id: []
})
/****************************** 组件参数 start ******************************/
// 获取参数
const props = defineProps({
  id: {
    type: String,
    default: null
  }
})

console.log(props, 'ssss')
/****************************** 组件参数 end ******************************/

// 调用查看接口获取信息
const getQuarantineOrderSee = async () => {
  //系统管理组织
  const Gzuzhi = await ucOrgListApi({ terminal_type: 3 })
  datas.admin_org_id = Gzuzhi.data
  // 畜牧局组织
  const Xzuzhi = await ucOrgListApi({ terminal_type: 2 })
  datas.ahb_org_id = Xzuzhi.data

  if (props.id) {
    const res = await ucIpOwnerSeeApi({ id: props.id })
      .catch(() => {})
      .finally(() => {})
    if (res) {
      data.value = res.data
    }
  }
}

defineExpose({ data, ruleFormRef, id: props.id })

/****************************** 联动数据 start ******************************/

// const AnbId = ref('')

/****************************** 联动数据 end ******************************/

/****************************** 初始化 start ******************************/
const init = async () => {
  // 调用查看接口查询数据
  await getQuarantineOrderSee()

  // 畜牧局组织
  const ahbOrgId = await ucDeptListApi({ org_id: data.value.ahb_org_id })
  datas.ahb_dept_id = ahbOrgId.data
  // 畜牧局部门
  const anbDeptId = await ucOrgDeptUserListApi({
    org_id: data.value.ahb_org_id,
    dept_id: data.value.ahb_dept_id
  })
  datas.ahb_user_id = anbDeptId.data
  // 系统组织
  const adminOrgId = await ucDeptListApi({ org_id: data.value.admin_org_id })
  datas.admin_dept_id = adminOrgId.data

  // 系统部门
  const adminDeptId = await ucOrgDeptUserListApi({
    org_id: data.value.admin_org_id,
    dept_id: data.value.admin_dept_id
  })
  datas.admin_user_id = adminDeptId.data
}
init()

// 系统管理组织
const aoi = async (a) => {
  if (a) {
    data.value.admin_dept_id = ''
    data.value.admin_user_id = ''
    datas.admin_user_id = []
    const adminOrgId = await ucDeptListApi({ org_id: a })
    datas.admin_dept_id = adminOrgId.data
    console.log(a, adminOrgId.data, '111')
  }
}
// 系统管理部门
const adi = async (a) => {
  // data.admin_user_id = ''
  if (a) {
    data.value.admin_user_id = ''
    const adminDeptId = await ucOrgDeptUserListApi({ org_id: data.value.admin_org_id, dept_id: a })
    datas.admin_user_id = adminDeptId.data
    // console.log(a, data.admin_org_id, datas.admin_user_id, '111')
  }
}

// 畜牧局组织
const ahoi = async (a) => {
  if (a) {
    data.value.ahb_dept_id = ''
    data.value.ahb_user_id = ''
    datas.ahb_user_id = []
    const ahbOrgId = await ucDeptListApi({ org_id: a })
    datas.ahb_dept_id = ahbOrgId.data
    // console.log(a, ahbOrgId.data, '111')
  }
}

// 系统畜牧局部门
const ahdi = async (a) => {
  // data.admin_user_id = ''
  if (a) {
    data.value.ahb_user_id = ''
    const anbDeptId = await ucOrgDeptUserListApi({ org_id: data.value.ahb_org_id, dept_id: a })
    datas.ahb_user_id = anbDeptId.data
    // console.log(a, data.ahb_org_id, '111')
  }
}
/****************************** 初始化 end ******************************/
</script>
<template>
  <ContentWrap title="主数据">
    <el-form
      ref="ruleFormRef"
      :rules="rules"
      :inline="true"
      label-position="right"
      label-width="auto"
      :model="data"
      class="demo-form-inline"
    >
      <el-form-item label="省份" prop="province">
        <el-input v-model="data.province" />
      </el-form-item>
      <el-form-item label="城市">
        <el-input v-model="data.city" />
      </el-form-item>

      <el-form-item label="系统管理组织" prop="admin_org_id">
        <el-select
          v-model="data.admin_org_id"
          placeholder="请选择系统管理组织"
          @change="aoi(data.admin_org_id)"
        >
          <el-option
            v-for="da in datas.admin_org_id"
            :key="da.org_id"
            :label="da.org_name"
            :value="da.org_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="畜牧局组织" prop="ahb_org_id">
        <el-select
          @change="ahoi(data.ahb_org_id)"
          v-model="data.ahb_org_id"
          placeholder="请选择畜牧局组织"
        >
          <el-option
            v-for="da in datas.ahb_org_id"
            :key="da.org_id"
            :label="da.org_name"
            :value="da.org_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="系统关系部门" prop="admin_dept_id">
        <el-tree-select
          @change="adi(data.admin_dept_id)"
          v-model="data.admin_dept_id"
          :data="datas.admin_dept_id"
          check-strictly
          :render-after-expand="false"
          node-key="dept_id"
          :props="{ label: 'dept_name', children: 'children' }"
        />
      </el-form-item>
      <el-form-item label="畜牧局部门" prop="ahb_dept_id">
        <el-tree-select
          @change="ahdi(data.ahb_dept_id)"
          v-model="data.ahb_dept_id"
          :data="datas.ahb_dept_id"
          check-strictly
          :render-after-expand="false"
          node-key="dept_id"
          :props="{ label: 'dept_name', children: 'children' }"
        />
      </el-form-item>
      <el-form-item label="系统管理人员" prop="admin_user_id">
        <el-select v-model="data.admin_user_id" placeholder="请选择系统管理人员">
          <el-option
            v-for="da in datas.admin_user_id"
            :key="da.user_id"
            :label="da.nickname"
            :value="da.user_id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="畜牧局人员" prop="ahb_user_id">
        <el-select v-model="data.ahb_user_id" placeholder="请选择畜牧局人员">
          <el-option
            v-for="da in datas.ahb_user_id"
            :key="da.user_id"
            :label="da.nickname"
            :value="da.user_id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="终端类型" prop="terminal_type">
        <el-select v-model="data.terminal_type" placeholder="请选择终端">
          <el-option
            v-for="da in dict.terminal_type"
            :key="da.value"
            :label="da.label"
            :value="da.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </ContentWrap>
</template>
